package com.site.blog.my.core.service.impl;

import com.site.blog.my.core.dao.BlogLinkMapper;
import com.site.blog.my.core.entity.BlogLink;
import com.site.blog.my.core.service.LinkService;
import com.site.blog.my.core.util.PageQueryUtil;
import com.site.blog.my.core.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author yz
 * @ClassNameLinkServiceImpl
 * @create 2021-07-07 9:15
 * @Version 1.0
 */
@Service
public class LinkServiceImpl implements LinkService
{
    @Autowired
    private BlogLinkMapper blogLinkMapper;
    @Override
    public PageResult getBlogLinkPage(PageQueryUtil pageUtil) {
       List<BlogLink> links=blogLinkMapper.findLinkList(pageUtil);
        int total=blogLinkMapper.getTotalLinks(pageUtil);
       PageResult  pageResult=new PageResult(links,total,pageUtil.getLimit(),pageUtil.getPage());
        return pageResult;
    }

    @Override
    public Map<Byte, List<BlogLink>> getLinksForLinkPage() {
        List<BlogLink> links=blogLinkMapper.findLinkList(null);
        if (!CollectionUtils.isEmpty(links)){
            Map<Byte, List<BlogLink>> linksMap=links.stream().collect(Collectors.groupingBy(BlogLink::getLinkType));
        return linksMap;
        }
        return null;
    }

}
